package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import service.line.LineService;
import service.majorunit.MajorunitService;
import service.region.RegionService;
import util.returnvo.LineReturnVO;
import util.returnvo.SearchListReturnVO;
import vo.RegionVO;

public class MajorContoller extends MultiActionController{
	
	private MajorunitService majorunitService;
	private LineService lineService;
	private RegionService regionService;
	
	public void setMajorunitService(MajorunitService majorunitService) {
		this.majorunitService = majorunitService;
	}
	
	public void setLineService(LineService lineService) {
		this.lineService = lineService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
	
	private ModelAndView defaultModelAndViewForSearch() throws Exception{
		List<LineReturnVO> lineList=lineService.getLineList();
		List<RegionVO> regionList=regionService.getAllRegion();
		
		ModelAndView mv=new ModelAndView("major/search");
		
		mv.addObject("lineList", lineList);
		mv.addObject("regionList", regionList);
		
		return mv;
	}

	/**
	 * 파라미터로 받은 검색값으로 service 호출해서 종합유닛리스트 리턴
	 */
	public ModelAndView searchMajorUnitByWord(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String search=request.getParameter("search");
		String column=request.getParameter("column");
		String univtype_no=request.getParameter("univtype_no");
		String page=request.getParameter("page");
		
		SearchListReturnVO unitList=majorunitService.getSearchedCompUnitByWord(search, column, univtype_no, page);
		
		ModelAndView mv=defaultModelAndViewForSearch();
		
		mv.addObject("unitList", unitList);
		
		return mv;
	}
	
	/**
	 * 파라미터로 받은 subline_no로 service 호출해서 종합유닛리스트 리턴
	 */
	public ModelAndView searchMajorUnitBySubline(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int subNo=Integer.parseInt(request.getParameter("subline_no"));
		String column=request.getParameter("column");
		String univtype_no=request.getParameter("univtype_no");
		String page=request.getParameter("page");
		
		SearchListReturnVO unitList=majorunitService.getSearchedCompUnitBySubline(subNo, column, univtype_no, page);
		
		ModelAndView mv=defaultModelAndViewForSearch();
		
		mv.addObject("unitList", unitList);
		
		return mv;
	}
	
	/**
	 * 파라미터로 받은 line_no로 service 호출해서 종합유닛리스트 리턴
	 */
	public ModelAndView searchMajorUnitByLine(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int line_no=Integer.parseInt(request.getParameter("line_no"));
		String column=request.getParameter("column");
		String univtype_no=request.getParameter("univtype_no");
		String page=request.getParameter("page");
		
		SearchListReturnVO unitList=majorunitService.getSearchedCompUnitByLine(line_no, column, univtype_no, page);
		
		ModelAndView mv=defaultModelAndViewForSearch();
		
		mv.addObject("unitList", unitList);
		
		return mv;
	}
	
	/**
	 * 파라미터로 받은 region_no로 service 호출해서 종합유닛리스트 리턴
	 */
	public ModelAndView searchMajorUnitByRegion(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int regionNo=Integer.parseInt(request.getParameter("region_no"));
		String column=request.getParameter("column");
		String univtype_no=request.getParameter("univtype_no");
		String page=request.getParameter("page");
		
		SearchListReturnVO unitList=majorunitService.getSearchedCompUnitByRegion(regionNo, column, univtype_no, page);
		
		ModelAndView mv=defaultModelAndViewForSearch();
		
		mv.addObject("unitList", unitList);
		
		return mv;
	}
	
	/**
	 * 검색값으로 자동완성리스트 보냄
	 */
	public ModelAndView autoComplete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String search=request.getParameter("search");
		
		List<String> sList=majorunitService.getAutoCompleteList(search);
		
		return new ModelAndView("JsonView", "sList", sList);
	}
	
	/**
	 * 검색값으로 자동완성리스트 보냄
	 */
	public ModelAndView getPopupSearchedUnit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String search=request.getParameter("search");
		String page=request.getParameter("page");
		
		SearchListReturnVO sList=majorunitService.getPopupSearchedUnit(search, page);
		
		return new ModelAndView("JsonView", "sList", sList);
	}
	
	/**
	 * 랭킹페이지로
	 */
	public ModelAndView ranking(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/ranking", "ranking", majorunitService.getRankingReturnVO());
	}
	
	public ModelAndView selectUnitPopup(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/select_unit_popup");
	}
}
